import 'package:elephant_fish/http/core/hi_net.dart';
import 'package:elephant_fish/http/request/base_request.dart';
import 'package:elephant_fish/http/request/reset_password_request.dart';
import 'package:elephant_fish/util/TxUtils.dart';
import 'package:elephant_fish/util/size_util.dart';
import 'package:elephant_fish/util/string_util.dart';
import 'package:elephant_fish/widget/text_fild.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'login_page.dart';

class ResetPasswordPage extends StatefulWidget {
  const ResetPasswordPage({Key key}) : super(key: key);

  @override
  _ResetPasswordPageState createState() => _ResetPasswordPageState();
}

class _ResetPasswordPageState extends State<ResetPasswordPage> {
  TextEditingController _nameController = TextEditingController();
  TextEditingController _passwordController = TextEditingController();
  TextEditingController _rePasswordController = TextEditingController();
  TextEditingController _vcodeController = TextEditingController();

  //分别定义两个输入框的焦点 用于切换焦点
  final FocusNode _nodeName = FocusNode();
  final FocusNode _nodePwd = FocusNode();
  final FocusNode _reNodePwd = FocusNode();
  final FocusNode _nodeVCode = FocusNode();
  bool _isClick = false;
  bool registerEnable = false;
  bool sample = false;
  String phone;
  String code;
  String password;
  String confirm;


  @override
  Widget build(BuildContext context) {
    final size = MediaQuery
        .of(context)
        .size;
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: SingleChildScrollView(
        child: Container(
          alignment: Alignment.topCenter,
          width: MediaQuery
              .of(context)
              .size
              .width,
          height: MediaQuery
              .of(context)
              .size
              .height,
          decoration: BoxDecoration(
            image: new DecorationImage(
              fit: BoxFit.cover,
              image: AssetImage('assets/images/register_bg.png'),
            ),
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [Color(0xffff9182), Color(0xffff849b)],
            ),

          ),
          child: Stack(
            children: [
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(top: SizeUtil.px(179)),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Image.asset(
                        'assets/images/logo.png', width: SizeUtil.px(57.72),
                        height: SizeUtil.px(56),),
                      SizedBox(width: SizeUtil.px(25),),
                      Text('象鱼直播', style: TextStyle(fontSize: SizeUtil.px(56),
                          color: Color(0xffffffff))),
                    ],
                  ),
                ),
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(left: SizeUtil.px(60),
                      right: SizeUtil.px(60),
                      top: SizeUtil.px(366)),
                  child: MyTextField(
                    focusNode: _nodeName,
                    placeHolder: '请输入您的手机号',
                    maxLength: 11,
                    keyboardType: TextInputType.phone,
                    controller: _nameController,
                    onChanged: (text) {
                      phone = text;
                      checkInput();
                    },
                  ),
                ),
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(left: SizeUtil.px(60),
                      right: SizeUtil.px(60),
                      top: SizeUtil.px(486)),
                  child: MyTextField(
                    focusNode: _nodeVCode,
                    placeHolder: '请输入验证码',
                    maxLength: 6,
                    controller: _vcodeController,
                    getVCode: (text) {
                      code = text;
                    },
                    keyboardType: TextInputType.phone,
                    onChanged: (text) {
                      code = text;
                      checkInput();
                    },
                  ),

                ),
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(left: SizeUtil.px(60),
                      right: SizeUtil.px(60),
                      top: SizeUtil.px(606)),
                  child: MyTextField(
                    focusNode: _nodePwd,
                    placeHolder: '请输入新密码',
                    controller: _passwordController,
                    isInputPwd: true,
                    onChanged: (text) {
                      password = text;
                      checkInput();
                    },
                  ),

                ),
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(left: SizeUtil.px(60),
                      right: SizeUtil.px(60),
                      top: SizeUtil.px(726)),
                  child: MyTextField(
                    focusNode: _reNodePwd,
                    placeHolder: '请重复输入新密码',
                    controller: _rePasswordController,
                    isInputPwd: true,
                    onChanged: (text) {
                      confirm = text;
                      checkInput();
                    },
                  ),

                ),
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                    padding: EdgeInsets.only(left: SizeUtil.px(60),
                        right: SizeUtil.px(60),
                        top: SizeUtil.px(950)),
                    child: Container(
                      width: SizeUtil.px(650),
                      height: SizeUtil.px(91),
                      child: RaisedButton(
                        shape: RoundedRectangleBorder(
                          side: BorderSide.none,
                          borderRadius: BorderRadius.all(Radius.circular(30)),
                        ),
                        color: Color(0xffe62d6c),
                        disabledColor: Color(0xffe62d6c),
                        child: Text(
                          '修改密码',
                          style: TextStyle(fontSize: SizeUtil.px(36),
                              color: Color(0xffffffff)),
                        ),
                        onPressed: () {
                          checkInput();
                          checkSample();
                          if (_isClick) {
                            if (registerEnable) {
                              setState(() {
                                reset();
                              });
                            } else {
                              setState(() {
                                TxUtils.showErrorToast('您填写的信息不正确', context);
                              });
                            }
                          } else {
                            setState(() {
                              TxUtils.showErrorToast('您还没有同意用户协议', context);
                            });
                          }
                        },
                      ),

                    )
                ),
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(top: SizeUtil.px(1061)),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Checkbox(value: _isClick,
                          onChanged: (bool value) {
                            setState(() {
                              _isClick = value;
                            });
                          },
                          checkColor: Color(0xffffffff),
                          materialTapTargetSize: MaterialTapTargetSize
                              .shrinkWrap),
                      SizedBox(width: SizeUtil.px(5),),
                      Text('我已看过并接受《用户协议》',
                          style: TextStyle(fontSize: SizeUtil.px(24),
                              color: Color(0xffffffff))),

                    ],
                  ),
                ),
              ),
            ],
          ),

        ),
      ),
    );
  }
  void checkInput() {
    bool enable;
    if (isNotEmpty(phone) && isNotEmpty(password) && isNotEmpty(code) && isNotEmpty(confirm) && sample) {
      enable = true;
    } else {
      enable = false;
    }
    setState(() {
      registerEnable = enable;
    });
  }

  void checkSample() {
    bool enable;
    if(Comparable.compare(password, confirm) == 0){
      enable = true;
    }else{
      enable = false;
    }
    setState(() {
      if(! enable){
        TxUtils.showToast('两次输入的密码不一致', context);
      }
      sample = enable;
    });
  }

  Future<void> reset() async {
    if(phone == '' || code == '' || password == '' || confirm == ''){
      TxUtils.showToast('您输入的信息不完整', context);
    }else{
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String appToken = prefs.getString('app-token');
      BaseRequest request = ResetPasswordRequest();
      request.add('phone', phone)
          .add('code', code)
          .add('password', password)
          .add('confirm', confirm)
          .addHeader('usertoken', appToken);
      var result = await HiNet.getInstance().fire(request);
      if (result['code'] == -1) {
        TxUtils.showErrorToast('登录失效，请重新登录', context);
        Navigator.pop(context);
        Navigator.of(context).push(MaterialPageRoute(
          builder: (context) {
            return LoginPage(
              arguments: {'loginCode': '-1'},
            );
          },
        ));
      } else if (result['code'] == 0) {
        TxUtils.showToast(result['data'], context);
        Navigator.pushNamed(context, '/login');
      }
      
    }
  }
}

